package android.slc.module.project.ui.browser

import android.slc.app.Bfxapp
import android.slc.base.ui.AppView
import android.slc.jxbrowser.JxFxmlBrowserView
import android.slc.module.project.repository.local.ProjectConfig
import android.slc.rx.SimpleSingleObserver
import android.slc.rx.SlcRxJavaUtils
import android.slc.utils.SlcFileUtils
import cn.hutool.core.util.StrUtil
import com.teamdev.jxbrowser.browser.callback.CreatePopupCallback
import com.teamdev.jxbrowser.engine.*
import io.reactivex.rxjava3.core.Single
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyCodeCombination
import javafx.scene.input.KeyCombination
import java.io.File
import java.util.concurrent.TimeUnit


class BrowserView : AppView<BrowserViewModel>() {

    lateinit var webView: JxFxmlBrowserView

    override fun onBindViewBefore() {
        super.onBindViewBefore()
        this.window.isMaximized = true
        this.window.isFullScreen = ProjectConfig.getInstance().browserViewFull
    }

    override fun onBindView() {
        super.onBindView()
        val escapeKc: KeyCombination = KeyCodeCombination(KeyCode.F11)
        screenRootPane.scene.accelerators[escapeKc] = Runnable {
            this.window.isFullScreen = !this.window.isFullScreen
            ProjectConfig.getInstance().browserViewFull = this.window.isFullScreen
        }
    }

    override fun onBindViewLater() {
        super.onBindViewLater()
        Single.timer(500, TimeUnit.MILLISECONDS)
            .compose(SlcRxJavaUtils.applyOsAndroidSchedulers())
            .to(viewModel.bindToLifecycle<Long>())
            .subscribe(object : SimpleSingleObserver<Long>() {
                override fun onSuccess(t: Long) {
                    super.onSuccess(t)
                    initWebBrowser()
                }
            })
    }

    private fun initWebBrowser() {
        val builder = EngineOptions.newBuilder(RenderingMode.HARDWARE_ACCELERATED)
        var userDataDirTemp = SlcFileUtils.complementSeparator(AppJxBrowserUtils.getUserDataDir())
        userDataDirTemp =
            userDataDirTemp + StrUtil.toUnderlineCase(Bfxapp.getCurrentPName()) + "_browser" + File.separator
        userDataDirTemp += StrUtil.toUnderlineCase(Bfxapp.getCurrentPName())
        val userDataDirFile = File(userDataDirTemp)
        builder.userDataDir(userDataDirFile.toPath())
        val browser = Engine.newInstance(
            builder.enableProprietaryFeature(ProprietaryFeature.AAC)
                .enableProprietaryFeature(ProprietaryFeature.H_264)
                .enableAutoplay()
                .language(Language.CHINESE)
                .build()
        ).newBrowser()
        browser.profile().preferences().enableAutofill()
        webView.loadBrowserView(browser)
        webView.browser().set(
            CreatePopupCallback::class.java,
            CreatePopupCallback { params ->
                webView.browser().navigation().loadUrl(params.targetUrl());
                CreatePopupCallback.Response.suppress();
            })

        webView.browser().navigation().loadUrl(ProjectConfig.getInstance().browserUrl)
    }

    override fun onDestroy() {
        webView.destroy()
        super.onDestroy()
    }
}